package edu.vt.ece_3574.amber_alert.accounts;


import java.util.ArrayList;

import com.google.android.maps.GeoPoint;

import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;

public class Account {
	/** The phone number associated with this account */
	private String number;
	
	/** The name associated with this account */
	private String name;
	
	/** The type of account */
	private String type;

	/** The password associated with this account */
	private String password;
	
	private GeoPoint lastLocation;
	
	private GeoPoint curfewLocation;
	
	private boolean locationUpdated;
	
	private String serialNumber;
	
	private ArrayList<String> associatedAccounts;
	
	private ArrayList<Friend> associatedFriends;
	

	/**
	 * Constructor. Creates an account with the specified information
	 * @param number the phone number for the account
	 * @param name the name for the account
	 * @param type the type of account
	 * @param password the password for the account
	 */
	public Account(String number, String name, String type, String password) {
		this.number = number;
		this.name = name;
		this.type = type;
		this.password = password;
		this.associatedAccounts = new ArrayList<String>();
		this.associatedFriends = new ArrayList<Friend>();
	}
	
	/**
	 * Adds an associated account to an account
	 * @param the associated account
	 */
	public void addAssociatedAccount(String accountName) {
		associatedAccounts.add(accountName);
		updateAccountOnServer();
	}
	
	public ArrayList<String> getAssociatedAccountNames() {
		return associatedAccounts;
	}
	
	public ArrayList<Account> getAssociatedAccounts() {
		ArrayList<Account> associatedAccounts = new ArrayList<Account>();
		for (String accountName : this.associatedAccounts) {
			associatedAccounts.add(DatabaseHandler.get().getAccountFromServerByName(accountName));
		}
		return associatedAccounts;
	}
	
 	public void setAssociatedAccounts(ArrayList<String> associatedAccounts) {
 		this.associatedAccounts = associatedAccounts;
 		updateAccountOnServer();
 	}
 	
 	public void removeAssociatedAccount(String accountName) {
 		associatedAccounts.remove(accountName);
 		updateAccountOnServer();
 	}
 	
 	public void addFriend(Friend friend) {
 		if (!associatedFriends.contains(friend)) {
 			associatedFriends.add(friend);
 			updateAccountOnServer();
 		}
	}
 	
 	public Friend getFriend(String name) {
 		for (Friend friend : associatedFriends) {
 			if (friend.getName().equals(name)) {
 				return friend;
 			}
 		}
 		return null;
 	}
 	
 	public ArrayList<String> getAssociatedFriendNames() {
 		ArrayList<String> friendNames = new ArrayList<String>();
 		for (Friend friend: associatedFriends) {
 			friendNames.add(friend.getName());
 		}
		return friendNames;
	}
 	
	public ArrayList<Friend> getAssociatedFriends() {
		return associatedFriends;
	}
	
	public void setAssociatedFriends(ArrayList<Friend> associatedFriends){
		this.associatedFriends = associatedFriends;
		updateAccountOnServer();
	}
	
	public void removeAssociatedFriend(String friendName) {
		associatedFriends.remove(friendName);
		updateAccountOnServer();
	}
	/**
	 * Gets the type of this account
	 * @return the type of account
	 */
	public String getType() {
		return type;
	}
	
	private void updateAccountOnServer() {
		DatabaseHandler.get().updateAccountOnServer(this);
	}
	
	/**
	 * Gets the phone number associated with this account
	 * @return the phone number
	 */
	public String getNumber() {
		return number;
	}
	
	/** 
	 * Gets the name associated with this account
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Gets the password associated with this account
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	
	/**
	 * Gets the last known location associated with this account
	 * @return the GeoPoint coordinates of the account's last known location
	 */
	public GeoPoint getLastLocation() {
		return lastLocation;
	}
	
	/**
	 * Sets the account's location
	 * @param location the GeoPoint location for the account
	 */
	public void setLocation(GeoPoint location) {
		lastLocation = location;
		updateAccountOnServer();
	}
	
	public boolean locationUpdated() {
		return locationUpdated;
	}
	
	public void setLocationUpdated(boolean updated) {
		locationUpdated = updated;
	}
	
	public GeoPoint getCurfewLocation() {
		return curfewLocation;
	}
	
	public void setCurfewLocation(GeoPoint location) {
		curfewLocation = location;
	}
	
	public String getSerialNumber() {
		return serialNumber;
	}
	
	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
		updateAccountOnServer();
	}
}
